<?php
/**
 * Create by 11li
 * Date 2020/1/8
 * Time 9:59
 *
 */

namespace app\api\controller\v1;



use app\common\controller\Api;
use app\common\service\GoodsService;
use app\common\service\VideoService;
use think\Exception;
use think\Request;

class Goods extends Api
{
	
	protected $noNeedLogin = '*';
	
	/**
	 * 商品列表
	 * @param Request $request
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function index( Request $request) {
		
		$keyword = $request->get('keyword');
		$cateId = $request->get('category_id');
		$page = $request->request('page', 1, 'intval');
		
		$obj = new GoodsService();
		$row = $obj->searchGoods($page, $keyword, $cateId);
		$this->success('查询成功', $row);
	}
	
	
	/**
	 * 商品详情
	 */
	public function detail($id) {
		if(!$id || intval($id) < 1) {
			$this->error('参数错误');
		}
		
		$obj = new GoodsService();
		$res = $obj->getGoodsDetail($id);
		if($res) {
			$this->success('查询成功', $res);
		} else {
			$this->error($obj->getError());
		}
	}
	
	/**
	 * 商品评价
	 */
	public function comment($id, $page) {
		if(!$id || intval($id) < 1) {
			$this->error('参数错误');
		}
		
		$obj = new GoodsService();
		try {
			$res = $obj->getGoodsComment($id, $page);
			$this->success('查询成功', $res);
		} catch (Exception $e) {
			$this->error($e->getMessage());
		}
	}
	
	/**
	 * 推荐商品
	 * @param Request $request
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function recommend(Request $request){
		$keyword = $request->get('keyword');
		$cateId = $request->get('category_id');
		
		$obj = new GoodsService();
		$row = $obj->searchGoods($request->get('page', 1, 'integer'), $keyword, $cateId);
		$this->success('查询成功', $row['list']);
	}
	

}